using System;
using System.Collections.Generic;
using System.Text;

namespace BlackjackGUI
{
   class CHand
   {
      #region Member Variables
      private LinkedList<CCard> m_cards;
      private int m_bet;
      private int m_total;
      private bool m_bust;
      private bool m_soft;
      #endregion

      #region Constructors & Destructor
      public CHand()
      {
         m_cards = new LinkedList<CCard>();
         m_bet = 0;
         m_bust = false;
      }

      //Destructor?
      ~CHand()
      {
         m_cards.Clear();
         m_cards = null;
      }
      #endregion

      #region Properties
      public int Bet
      {
         get
         {
            return m_bet;
         }
         set
         {
            m_bet = value;
         }
      }

      public bool Bust
      {
         get
         {
            return m_bust;
         }
         set
         {
            m_bust = value;
         }
      }

      public bool Blackjack
      {
         get
         {
            return (m_cards.Count == 2 && Total == 21);
         }
      }

      public int Count
      {
         get
         {
            return m_cards.Count;
         }
      }

      public int Total
      {
         get
         {
            return m_total;
         }
         set
         {
            m_total = value;
         }
      }

      public bool SoftHand
      {
         get
         {
            return m_soft;
         }
      }

      public bool Splitable
      {
         get
         {
            if (Count == 2)
            {
               ValueEnum first = this[0].GetValue();
               ValueEnum second = this[1].GetValue();
               if ((first == second) || 
                   ((first == ValueEnum.TEN || first == ValueEnum.JACK || 
                     first == ValueEnum.QUEEN || first == ValueEnum.KING) && 
                     (second == ValueEnum.TEN || second == ValueEnum.JACK || 
                      second == ValueEnum.QUEEN || second == ValueEnum.KING)))
               {
                  return true;
               }
            }
            return false;
         }
      }
      #endregion

      #region Accessors
      public CCard this[int index]
      {
         get 
         {
            if ((m_cards.Count == 0) || (index >= m_cards.Count))
            {
               return null;
            }
            else
            {
               LinkedListNode<CCard> iter = m_cards.First;
               for (int ii = 0; ii < index; ii++)
               {
                  iter = iter.Next;
               }
               return iter.Value;
            }
         }
      }

      private int lTotal()
      {
         int retVal = 0;
         m_soft = false;
         if (m_cards.Count > 0)
         {
            for (int ii = 0; ii < m_cards.Count; ii++)
            {
               ValueEnum currVal = this[ii].GetValue();
               if (currVal == ValueEnum.ACE)
               {
                  if (retVal > 10)
                  {
                     retVal += 1;
                  }
                  else
                  {
                     retVal += 11;
                     m_soft = true;
                  }
               }
               else if (currVal == ValueEnum.KING || currVal == ValueEnum.QUEEN || currVal == ValueEnum.JACK)
               {
                  retVal += 10;
               }
               else
               {
                  retVal += (int)currVal;
               }
            }

            // If we busted with an Ace as 11, recount it as 1
            if (retVal > 21 && m_soft)
            {
               retVal = 0;
               for (int ii = 0; ii < m_cards.Count; ii++)
               {
                  ValueEnum currVal = this[ii].GetValue();
                  if (currVal == ValueEnum.ACE)
                  {
                     retVal += 1;
                  }
                  else if (currVal == ValueEnum.KING || currVal == ValueEnum.QUEEN || currVal == ValueEnum.JACK)
                  {
                     retVal += 10;
                  }
                  else
                  {
                     retVal += (int)currVal;
                  }
               }
            }
         }
         return retVal;
      }
      #endregion

      #region Mutators
      public void Clear()
      {
         m_cards.Clear();
         m_bet = 0;
         m_bust = false;
      }

      public void AddCard(CCard card)
      {
         m_cards.AddLast(card);
         Total = lTotal();
         if (Total > 21)
         {
            m_bust = true;
         }
      }

      public void RemoveLast()
      {
         m_cards.RemoveLast();
      }
      #endregion



	
   }
}
